package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.Help.HelpCenterMapper;
import com.weipai.demo.model.HelpCenter;
import com.weipai.demo.service.HelpCenterService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/7/6 11:52
 * @Version 1.0
 */
@Service
public class HelpCenterServiceImpl implements HelpCenterService {

    private static final Logger log = LoggerFactory.getLogger(LoansManagerServiceImpl.class);

    @Autowired
    private HelpCenterMapper helpCenterMapper;

    //帮助中心介绍填写和修改
    @Override
    public Response save(HelpCenter helpCenter) {
        Response response = new Response();

        List<HelpCenter> helpCenters = helpCenterMapper.selectAll();
        if (helpCenters.size()==1){
            int i = helpCenterMapper.updateByPrimaryKey(helpCenter);
            if (i > 0) {
                response.setCode(RetCode.SUCCESS);
                return response;
            }

        }else {
            if (helpCenter.getId() == null) {
                int insert = helpCenterMapper.insert(helpCenter);
                if (insert > 0) {
                    response.setCode(RetCode.SUCCESS);
                }
            }
        }

        return response;

    }

    //展示帮助中心
    @Override
    public Response show() {
        Response response=new Response();
        List<HelpCenter> helpCenters = helpCenterMapper.selectAll();
        response.setCode(RetCode.SUCCESS);
        response.put("helpCenters",helpCenters);
        return response;
    }

    //根据id删除帮助中心
    @Override
    public Response deleteById(Integer id) {
        Response response=new Response();
        try {
            HelpCenter helpCenter=new HelpCenter();
            helpCenter.setId(id);
            int delete = helpCenterMapper.delete(helpCenter);
            if (delete>0){
                response.setCode(RetCode.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("deleteById"+e.getMessage());
        }
        return response;
    }
}
